<?php

namespace api\modules\v1\models;
use Yii;
use api\common\models\User;
use api\func\func;
use yii\db\Query;
use yii\data\Pagination;
use yii\web\HttpException;
use yii\web\Link;
use api\common\models\Common;
class Ucenter extends Common
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%article_app}}';
    }

    /*
     * 头部公共信息
     * @return array
     */
    public function layout_info(){
        $data = [];
        $user = $this->checkLoginAuth();
        $data['user'] = $this->getUserInfo();

        $data['fans_count'] = (new Query())
            ->from('{{%fans_app}} as a')
            ->innerJoin('{{%user_app}} as b','a.user = b.id')
            ->where(['a.to_user'=>$user['id']])
            ->count();

        //$data['fans_count'] =  Fans::find() ->where(['to_user'=>$user['id']])->innerJoin('{{%user_app}} as b','a.user = b.id')->count();



        $data['attention_count'] = (new Query())
            ->from('{{%fans_app}} as a')
            ->innerJoin('{{%user_app}} as b','a.to_user = b.id')
            ->where(['a.user'=>$user['id']])
            ->count();

        //$data['attention_count'] = Fans::find()->where(['user'=>$user['id']])->count();



        $this->visitorAdd();
        return $data;
    }

    /*
     * 获取用户基本信息
     */
    public function getUserInfo(){
        $user = $this->checkLoginAuth();
        $_user = User::findOne($user['id']);
        $user = [];
        $user['nickname'] = $_user->nickname != '' ? $_user->nickname : func::replace_phone($_user->phone);
        $user['descript'] = $_user->is_authpublish == 1 ? ($_user->descript !='' ? $_user->descript : Yii::$app->params['default_descript']) : Yii::$app->params['default_descript'];
        $user['head_img'] = $_user->head_img != '' ? $_user->head_img : Yii::$app->params['default_header'];//需求默认头像
        $user['is_authpublish'] = $_user->is_authpublish;
        return $user;
    }

    /*
     * 组织首页需要的数据信息
     * @return array
     */
    public function getIndex(){
        $data = [];
        $data['article'] = $this->getArticleAndComment();
        return $data;
    }

    public function getArticleAndComment(){
        $user = $this->checkLoginAuth();
        $query = ((new Query())
            ->select('*')
            ->from("(
                        (
                            SELECT
                                c.id,
                                c.pubdate as time,
                                CONCAT('article') as type
                            FROM
                                {{%article_app}} as c
                                INNER JOIN {{%news}} e ON c.newsid = e.id
                            WHERE
                                c.author = {$user['id']} and c.`status` = 2 and e.status = 99
                        )
                        UNION ALL
                            (
                                SELECT
                                    c.comment_id,
                                    c.time as time,
                                    CONCAT('comment') as type
                                FROM
                                    {{%comment_app}} as c
                                    INNER JOIN {{%user_app}} b ON c.uid = b.id
                                    INNER JOIN {{%news}} e ON (c.content_id = e.`id` and c.catid = e.`catid`)
                                WHERE
                                    c.uid = {$user['id']} and c.`status` = 1  and e.status = 99
                            )
                    ) as a"
                )
        );
        $query->orderBy('a.time desc');
        $count = $query->count();

        $pagination = new Pagination(['totalCount' => $count]);
        $pagination->defaultPageSize = 10;
        $data_tmp = $query->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();
        $data = [];
        $HtmlBaseUrl = self::getHtmlBaseUrl();

        foreach($data_tmp as $value){
            if($value['type'] == 'article'){
                $fields = 'b.id,b.catid,b.title,b.description,b.inputtime,b.zst,b.look,b.comment,b.url,a.istop,c.id as is_recommend,count(d.id) as collect_num,e.catname as catname';
                $query = ((new Query())
                    ->select($fields)
                    ->from('{{%article_app}} as a') //是否置顶
                    ->innerJoin('{{%news}} as b','a.newsid = b.id')
                    ->leftJoin('{{%position_data}} as c','(b.id = c.id) and (b.catid = c.catid)') //是否被推荐
                    ->leftJoin('{{%collect_app}} as d','(b.id = d.content_id) and (b.catid = d.catid)') //收藏量
                    ->leftJoin('{{%category}} as e','(b.catid = e.catid)') //分类名称
                    ->where('a.id='.$value['id'])
                );
                $_data = $query->one();
                if($_data['id'] == '' ){
                    continue;
                }
                $_data['type'] = 'article';
                $_data['zst'] = json_decode($_data['zst'],1);
                $_data['is_recommend'] = $_data['is_recommend']!=''? 1 : 0;

                if ($_data['url'] != '' && substr($_data['url'], -5) == '.html') {
                    $app_url = parse_url($_data['url']);
                    $app_url = $HtmlBaseUrl . $app_url['path'];
                    $app_url = str_replace('/html/', '/app/', $app_url);
                    $_data['url'] = $app_url;
                }

                if(is_array($_data['zst']) && count($_data['zst']) > 0){
                    $zst = array_column($_data['zst'],'url');
                    $zst = implode(',',$zst);
                }else{
                    $zst = '';
                }
                $_data['zst'] = $zst;
                $_data['time'] = date('Y-m-d H:i:s',$_data['inputtime']);

                $data[] = $_data;
            }elseif($value['type'] == 'comment'){

                $fields = 'a.comment_id,a.uid,a.top_pid,a.pid,a.content,a.content_id,a.catid,,a.time,a.like,b.title,b.description,b.zst,b.url,b.thirdid,c.nickname,c.phone,c.`head_img`,c.descript';
                $query = ((new Query())
                    ->select($fields)
                    ->from('{{%comment_app}} as a')
                    ->innerJoin('{{%user_app}} as c','a.uid = c.`id`')
                    ->innerJoin('{{%news}} as b','(a.content_id = b.`id` and a.catid = b.`catid`)')
                    ->where('a.comment_id='.$value['id'])
                );
                $_data = $query->one();
                if($_data['comment_id'] == '' ){
                    continue;
                }
                $_data['type'] = 'comment';
                $_data['nickname'] = $_data['nickname'] != '' ? $_data['nickname'] : func::replace_phone($_data['phone']);
                $_data['descript'] = $_data['descript'] !='' ? $_data['descript'] : Yii::$app->params['default_descript'];
                $_data['head_img'] = $_data['head_img'] != '' ? $_data['head_img'] : Yii::$app->params['default_header'];//需求默认头像
                if ($_data['url'] != '' && substr($_data['url'], -5) == '.html') {
                    $app_url = parse_url($_data['url']);
                    $app_url = $HtmlBaseUrl . $app_url['path'];
                    $app_url = str_replace('/html/', '/app/', $app_url);
                    $_data['url'] = $app_url;
                }

                $zst = json_decode($_data['zst'],1);
                if(count($zst) > 0){
                    $_data['zst'] = $zst[0]['url'];
                }else{
                    $_data['zst'] = '';
                }

                $_data['islike'] = '0';

                Like::$_tableName = Like::tableExists(self::formatTable($_data['content_id'],Yii::$app->params['table_like']['num'],Yii::$app->params['table_like']['table']));

                $islike = (new \yii\db\Query())->from(Like::$_tableName)->select('uid')->where(['uid'=>$user['id'],'comment_id'=>$_data['comment_id'],'position_type'=>2,'catid'=>$_data['catid']])->one(); //查找自己是否已经点赞
                if($islike && $islike['uid'] != ''){
                    $_data['islike'] = '1';
                }
                $_data['at'] = '';

                if($_data['pid'] != 0){
                    if($p_data = Comment::findOne(['comment_id'=>$_data['pid']])){
                        if($u_data = User::findOne(['id'=>$p_data->uid])){
                            $u_data->nickname = $u_data->nickname != '' ? $u_data->nickname : func::replace_phone($u_data->phone);

                            $_data['at'] = $u_data->nickname;
                        }else{
                            $_data['at'] = '用户已删除';
                        }
                    }else{
                        $_data['at'] = '上级评论已删除';
                    }
                }

                $_data['child_top_pid'] = $_data['top_pid']!=0 ? $_data['top_pid'] : $_data['comment_id'];
                $data[] = $_data;
            }
        }

        return ['data'=>$data,'pages'=>$this->_serializePagination($pagination)];
    }


    /*
     * 获取发布文章信息
     * @return array
     * @throws HttpException
     */
    public function getArticle(){
        $user = $this->checkLoginAuth();
        $fields = 'b.id,b.catid,b.title,b.description,b.inputtime,b.zst,b.look,b.comment,b.url,a.istop,c.id as is_recommend,count(d.id) as collect_num,e.catname as catname';
        $where = ['a.author'=>$user['id'],'a.status'=>2,'b.`status`'=>99];
        $query = ((new Query())
            ->select($fields)
            ->from('{{%article_app}} as a') //是否置顶
            ->innerJoin('{{%news}} as b','a.newsid = b.id')
            ->leftJoin('{{%position_data}} as c','(b.id = c.id) and (b.catid = c.catid)') //是否被推荐
            ->leftJoin('{{%collect_app}} as d','(b.id = d.content_id) and (b.catid = d.catid)') //收藏量
            ->leftJoin('{{%category}} as e','(b.catid = e.catid)') //分类名称
            ->where($where)
            ->groupBy('b.id')
        );
        $query->orderBy('a.istop desc,b.id desc');
        $count = $query->count();

        $pagination = new Pagination(['totalCount' => $count]);
        $pagination->defaultPageSize = 10;
        $data = $query->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();
        //print_r($query->createCommand()->getRawSql());exit;

        $HtmlBaseUrl = self::getHtmlBaseUrl();
        foreach($data as &$value){
            $value['zst'] = json_decode($value['zst'],1);
            $value['is_recommend'] = $value['is_recommend']!=''? 1 : 0;

            if ($value['url'] != '' && substr($value['url'], -5) == '.html') {
                $app_url = parse_url($value['url']);
                $app_url = $HtmlBaseUrl . $app_url['path'];
                $app_url = str_replace('/html/', '/app/', $app_url);
                $value['url'] = $app_url;
            }

            $value['type'] = 'article';
        }
        //print_r($data);exit;
        return ['data'=>$data,'pages'=>$this->_serializePagination($pagination)];
    }

    /*
     * 获取粉丝列表
     */
    public function getFansList(){
        $user = $this->checkLoginAuth();
        $fields = 'a.`user`,a.to_user,a.time,b.nickname,b.phone,b.head_img,b.descript';
        $where = "a.to_user = {$user['id']}";
        $query = ((new Query())
            ->select($fields)
            ->from('{{%fans_app}} as a')
            ->innerJoin('{{%user_app}} as b','a.user = b.id')
            ->where($where)
        );

        $query->orderBy('a.time desc');

        $count = $query->count();

        $pagination = new Pagination(['totalCount' => $count]);
        $pagination->defaultPageSize = 10;
        $data = $query->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();

        $_data = [];
        if(count($data) > 0){
            $my_user = $this->getTokenUserId();
            $is_hfs = [];
            if($my_user != ''){
                $is_hf_id = implode(',',array_column($data,'user'));
                $is_hfs = Fans::find()->select('to_user')->where("user=".$my_user['id']." and to_user in (".$is_hf_id.")")->asArray()->all();
                $is_hfs = array_unique(array_column($is_hfs,'to_user'));
            }

            foreach($data as $key=>$value){
                $data[$key]['nickname'] = $value['nickname'] != '' ? $value['nickname'] : func::replace_phone($value['phone']);
                $data[$key]['descript'] = $value['descript'] !='' ? $value['descript'] : Yii::$app->params['default_descript'];
                $data[$key]['head_img'] = $value['head_img'] != '' ? $value['head_img'] : Yii::$app->params['default_header'];//需求默认头像
                if(count($is_hfs) > 0){
                    if(in_array($value['user'],$is_hfs)){
                        $data[$key]['is_hf'] = 1;
                    }else{
                        $data[$key]['is_hf'] = 0;
                    }
                }else{
                    $data[$key]['is_hf'] = 0;
                }
            }

            foreach($data as $key=>$value){
                $_data[$key]['fans_uid'] = $value['user'];
                $_data[$key]['fans_time'] = $value['time'];
                $_data[$key]['is_hf'] = $value['is_hf'];
                $_data[$key]['fans_info']['id'] = $value['user'];
                $_data[$key]['fans_info']['nickname'] = $value['nickname'];
                $_data[$key]['fans_info']['descript'] = $value['descript'];
                $_data[$key]['fans_info']['phone'] = $value['phone'];
                $_data[$key]['fans_info']['head_img'] = $value['head_img'];
            }
        }
        return ['data'=>$_data,'pages'=>$this->_serializePagination($pagination)];
    }

    /*
     * 取消互粉
     * @param string $to_user
     */
    public function FansDel($to_user=''){
        if($to_user == ''){
            throw new HttpException(401,'参数不完整',0);
        }
        $model = new Fans();
        $uid = Yii::$app->user->identity->id;
        if($model->deleteAll(['to_user'=>$to_user,'user'=>$uid])){
            return ['msg'=>'取消互粉成功!'];
        }elseif (!$model->hasErrors()) {
            throw new HttpException(401,'由于不明原因未能创建对象',0);
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }

    /*
     * 再次互粉
     */
    public function FansAdd($to_user=''){
        $uid = Yii::$app->user->identity->id;

        if($to_user == ''){
            throw new HttpException(401,'参数不完整',0);
        }

        if($uid == $to_user){
            throw new HttpException(401,'不能互粉本身',0);
        }

        $model = new Fans();
        $model->user = $uid;
        $model->to_user = $to_user;
        $model->time = date('Y-m-d H:i:s');
        if($model->findOne(['user'=>$uid,'to_user'=>$to_user])){
            throw new HttpException(401,'互粉状态已经存在',0);
        }
        if($model->save()){
            return ['msg'=>'互粉成功!'];
        }elseif (!$model->hasErrors()) {
            throw new HttpException(401,'由于不明原因未能创建对象',0);
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }

    /*
     * 关注列表
     * @return array
     * @throws HttpException
     */
    public function getAttentionList(){
        $user = $this->checkLoginAuth();
        $uid = $user['id'];
        $fields = 'a.`user`,a.to_user,a.time,b.nickname,b.phone,b.head_img,b.descript';
        $where = "a.user = {$uid} and b.phone != ''";
        $query = ((new Query())
            ->select($fields)
            ->from('{{%fans_app}} as a')
            ->innerJoin('{{%user_app}} as b','a.to_user = b.id')
            ->where($where)
        );

        $query->orderBy('a.time desc');

        $count = $query->count();

        $pagination = new Pagination(['totalCount' => $count]);
        $pagination->defaultPageSize = 10;
        $data = $query->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();

        if(count($data) > 0){
            $my_user = $this->getTokenUserId();
            $is_hfs = [];
            if($my_user != ''){
                $is_hf_id = implode(',',array_column($data,'to_user'));
                $is_hfs = Fans::find()->select('to_user')->where("user=".$my_user['id']." and to_user in (".$is_hf_id.")")->asArray()->all();
                $is_hfs = array_unique(array_column($is_hfs,'to_user'));
            }

            foreach($data as &$value){
                $value['nickname'] = $value['nickname'] != '' ? $value['nickname'] : func::replace_phone($value['phone']);
                $value['descript'] = $value['descript'] !='' ? $value['descript'] : Yii::$app->params['default_descript'];
                $value['head_img'] = $value['head_img'] != '' ? $value['head_img'] : Yii::$app->params['default_header'];//需求默认头像
                if(count($is_hfs) > 0){
                    if(in_array($value['to_user'],$is_hfs)){
                        $value['is_hf'] = 1;
                    }else{
                        $value['is_hf'] = 0;
                    }
                }else{
                    $value['is_hf'] = 0;
                }
            }
        }
        return ['data'=>$data,'pages'=>$this->_serializePagination($pagination)];
    }

    /*
     * 取消关注
     * @param $aid
     * @return array
     */
    public function AttentionDel($to_user){
        $uid = Yii::$app->user->identity->id;
        if($to_user == ''){
            throw new HttpException(401,'参数不完整',0);
        }
        $model = new Fans();
        if($model->deleteAll(['to_user'=>$to_user,'user'=>$uid])){
            return ['msg'=>'取消关注成功!'];
        }elseif (!$model->hasErrors()) {
            throw new HttpException(401,'由于不明原因未能创建对象',0);
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }

    /*
     * 再次关注
     * @param $aid
     * @return array
     */
    public function AttentionAdd($to_user){
        $uid = Yii::$app->user->identity->id;
        if($to_user == ''){
            throw new HttpException(401,'参数不完整',0);
        }

        if($uid == $to_user){
            throw new HttpException(401,'不能关注本身',0);
        }

        $model = new Fans();
        $model->user = $uid;
        $model->to_user = $to_user;
        $model->time = date('Y-m-d H:i:s');
        if($model->findOne(['user'=>$uid,'to_user'=>$to_user])){
            throw new HttpException(401,'关注状态已经存在',0);
        }
        if($model->save()){
            $sendAttention_model = new Attention();
            $sendAttention_model->sendAttentionMsg($to_user,$uid);
            return ['msg'=>'关注成功!'];
        }elseif (!$model->hasErrors()) {
            throw new HttpException(401,'由于不明原因未能创建对象',0);
        }else{
            return ['msg'=>array_values($model->getFirstErrors())[0]];
        }
    }

    public function VisitorFansDel($to_user){
        return $this->AttentionDel($to_user);
    }

    public function VisitorFansAdd($to_user){
        return $this->AttentionAdd($to_user);
    }

    /*
     * 访客列表
     * @throws HttpException
     */
    public function Visitor(){
        $user = $this->checkLoginAuth();
        $fields = 'a.visitor_uid,a.time,b.`id`,b.nickname,b.phone,b.`head_img`,b.descript';
        $where = "a.uid = {$user['id']}";
        $query = ((new Query())
            ->select($fields)
            ->from('{{%visitor_app}} as a')
            ->innerJoin('{{%user_app}} as b','a.visitor_uid = b.`id`')
            ->where($where)
        );

        $query->orderBy('a.time desc');
        $count = $query->count();

        $pagination = new Pagination(['totalCount' => $count]);
        $pagination->defaultPageSize = 10;
        $_data = $query->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();
        $data = [];
        if(count($_data) > 0){
            $my_user = $this->getTokenUserId();
            $is_hfs = [];
            if($my_user != ''){
                $is_hf_id = implode(',',array_column($_data,'visitor_uid'));
                $is_hfs = Fans::find()->select('to_user')->where("user=".$my_user['id']." and to_user in (".$is_hf_id.")")->asArray()->all();
                $is_hfs = array_unique(array_column($is_hfs,'to_user'));
            }

            foreach($_data as $key=>$value){
                $data[$key]['visitor_uid'] = $value['visitor_uid'];
                $data[$key]['time'] = $value['time'];
                $data[$key]['nickname'] = $value['nickname'] != '' ? $value['nickname'] : func::replace_phone($value['phone']);
                $data[$key]['descript'] = $value['descript'] !='' ? $value['descript'] : Yii::$app->params['default_descript'];
                $data[$key]['head_img'] = $value['head_img'] != '' ? $value['head_img'] : Yii::$app->params['default_header'];//需求默认头像
                if(count($is_hfs) > 0){
                    if(in_array($value['visitor_uid'],$is_hfs)){
                        $data[$key]['is_hf'] = 1;
                    }else{
                        $data[$key]['is_hf'] = 0;
                    }
                }else{
                    $data[$key]['is_hf'] = 0;
                }
            }
        }
        return ['data'=>$data,'pages'=>$this->_serializePagination($pagination)];
    }

    /*
     * 评论列表
     * @return array
     * @throws HttpException
     */
    public function CommentList(){
        $user = $this->checkLoginAuth();
        $fields = 'a.comment_id,a.uid,a.top_pid,a.pid,a.content,a.content_id,a.catid,,a.time,a.like,b.title,b.zst,b.url,b.thirdid,c.nickname,c.phone,c.`head_img`,c.descript,b.description';
        $where = "a.uid = {$user['id']} and a.status=1 and b.`status`=99 ";
        $query = ((new Query())
            ->select($fields)
            ->from('{{%comment_app}} as a')
            ->innerJoin('{{%user_app}} as c','a.uid = c.`id`')
            ->innerJoin('{{%news}} as b','(a.content_id = b.`id` and a.catid = b.`catid`)')
            ->where($where)
        );

        $query->orderBy('a.time desc');

        $count = $query->count();

        $pagination = new Pagination(['totalCount' => $count]);
        $pagination->defaultPageSize = !empty($_POST['comment_pagesize']) ? $_POST['comment_pagesize'] : 10;
        $data = $query->offset($pagination->offset)
            ->limit($pagination->limit)
            ->all();
        $HtmlBaseUrl = self::getHtmlBaseUrl();
        foreach($data as $key=>$value){
            $data[$key]['nickname'] = $value['nickname'] != '' ? $value['nickname'] : func::replace_phone($value['phone']);
            $data[$key]['descript'] = $value['descript'] !='' ? $value['descript'] : Yii::$app->params['default_descript'];
            $data[$key]['head_img'] = $value['head_img'] != '' ? $value['head_img'] : Yii::$app->params['default_header'];//需求默认头像
            $zst = json_decode($value['zst'],1);
            if(count($zst) > 0){
                $data[$key]['zst'] = $zst[0]['url'];
            }else{
                $data[$key]['zst'] = '';
            }

            /*查找自己是否点赞*/
            $data[$key]['islike'] = '0';

            Like::$_tableName = Like::tableExists(self::formatTable($value['content_id'],Yii::$app->params['table_like']['num'],Yii::$app->params['table_like']['table']));

            $islike = (new \yii\db\Query())->from(Like::$_tableName)->select('uid')->where(['uid'=>$user['id'],'comment_id'=>$value['comment_id'],'position_type'=>2,'catid'=>$value['catid']])->one(); //查找自己是否已经点赞
            if($islike && $islike['uid'] != ''){
                $data[$key]['islike'] = '1';
            }
            /*查找自己是否点赞*/

            $data[$key]['at'] = '';
            if($value['pid'] != 0){
                if($p_data = Comment::findOne(['comment_id'=>$value['pid']])){
                    if($u_data = User::findOne(['id'=>$p_data->uid])){
                        $u_data->nickname = $u_data->nickname != '' ? $u_data->nickname : func::replace_phone($u_data->phone);
                        $data[$key]['at'] = $u_data->nickname;
                    }else{
                        $data[$key]['at'] = '用户已删除';
                    }
                }else{
                    $data[$key]['at'] = '上级评论已删除';
                }
            }

            if ($value['url'] != '' && substr($value['url'], -5) == '.html') {
                $app_url = parse_url($value['url']);
                $app_url = $HtmlBaseUrl . $app_url['path'];
                $app_url = str_replace('/html/', '/app/', $app_url);
                $data[$key]['url'] = $app_url;
            }

            $data[$key]['child_top_pid'] = $value['top_pid']!=0 ? $value['top_pid'] : $value['comment_id'];

            $data[$key]['type'] = 'comment';
        }

        return ['data'=>$data,'pages'=>$this->_serializePagination($pagination)];
    }

    /*
     * 访客数量
     * @return mixed
     */
    public function VisitorHits(){
        $user = $this->checkLoginAuth();
        $query = new \yii\db\Query();
        $data = [];
        $data['all'] = $query->from('{{%visitor_history_app}}')->where(['uid'=>$user['id']])->count();//总共数量
        $_day = date('Y-m-d',strtotime('-7 days'));
        $data['seven'] = $query->from('{{%visitor_history_app}}')->where('uid = '.$user['id'].' and time >="'.$_day.'"')->count();//总共数量
        return $data;
    }


    public function checkLoginAuth(){
        if(isset(Yii::$app->user->identity->id) && Yii::$app->user->identity->id !=''){
            $uid = Yii::$app->user->identity->id;
        }else{
            throw new HttpException(401,'未获取到用户ID',0);
        }
        $query = new \yii\db\Query();
        if($data = $query->from('{{%user_app}}')->select('id,username,collect_category,nickname,phone,score')->where(['id'=>$uid])->one()){
            return $data;
        }else{
            throw new HttpException(401,'该用户不存在',0);
        }
    }

    public function visitorAdd(){
        $user_info = $this->getTokenUserId();
        if($user_info != '' && Yii::$app->request->get('uid','') !=''){
            $uid = $user_info['id'];
            $visitor_uid = Yii::$app->request->get('uid','');
            $visitor = new Visitor();
            if($uid != $visitor_uid){
                $visitor->add($uid,$visitor_uid);
                $visitor->history($uid,$visitor_uid);
            }
        }
    }

    public function _serializePagination($pagination)
    {
        $pages = [];
        $pages['linksEnvelope'] = Link::serialize($pagination->getLinks(true));
        $pages['metaEnvelope'] = [
            'totalCount' => $pagination->totalCount,
            'pageCount' => $pagination->getPageCount(),
            'currentPage' => $pagination->getPage() + 1,
            'perPage' => $pagination->getPageSize(),
        ];
        return $pages;
    }

}